package com.example.saifly.inPageRecord.BodyHistoryData;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.RecordModel.BodyData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class BodyHistoryCarbonDioxidePressureActivity extends Activity {
    @Override
    protected int getContentLayoutId()
    {
        return R.layout.activity_carbondioxidepressure_history;
    }
    private ImageView ivReturn;
    private int carbonDioxidePressure[]=new int[5];
    private String inputTime[]=new String[5];
    private List<BodyData> CarbonDioxidePressureList;

    private TextView tvInputTime1;
    private TextView tvInputTime2;
    private TextView tvInputTime3;
    private TextView tvInputTime4;
    private TextView tvInputTime5;
    private TextView tvCarbonDioxidePressureData1;
    private TextView tvCarbonDioxidePressureData2;
    private TextView tvCarbonDioxidePressureData3;
    private TextView tvCarbonDioxidePressureData4;
    private TextView tvCarbonDioxidePressureData5;

    private SharedPreferences sharedPreferences= MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

    @Override
    protected void init()
    {
        super.init();

        ivReturn = findViewById(R.id.carbonDioxidePressureData_return);

        CarbonDioxidePressureList=new ArrayList<>();

        tvInputTime1=findViewById(R.id.carbonDioxidePressureTime1);
        tvInputTime2=findViewById(R.id.carbonDioxidePressureTime2);
        tvInputTime3=findViewById(R.id.carbonDioxidePressureTime3);
        tvInputTime4=findViewById(R.id.carbonDioxidePressureTime4);
        tvInputTime5=findViewById(R.id.carbonDioxidePressureTime5);
        tvCarbonDioxidePressureData1=findViewById(R.id.carbonDioxidePressureData1);
        tvCarbonDioxidePressureData2=findViewById(R.id.carbonDioxidePressureData2);
        tvCarbonDioxidePressureData3=findViewById(R.id.carbonDioxidePressureData3);
        tvCarbonDioxidePressureData4=findViewById(R.id.carbonDioxidePressureData4);
        tvCarbonDioxidePressureData5=findViewById(R.id.carbonDioxidePressureData5);

        for(int i=0;i<5;i++)
        {
            carbonDioxidePressure[i]=0;
            inputTime[i]="未录入";
        }

        new Thread(new Runnable() {
            @Override
            public void run()
            {
                carbonDioxidePressureHistory(sharedPreferences.getString("uid","default"));
            }
        }).start();

        doJump();

    }

    private interface getTopBodyData {
        @Headers({"Content-Type:application/json"})
        @POST("/estimate/getTopBodyData")
        Call<ResponseBody> carbonDioxidePressureHistory(@Body JSONObject jsonObject);
    }

    private void carbonDioxidePressureHistory(String phoneNumber) {
        RetrofitHelper retrofitHelper = new RetrofitHelper();
        Retrofit retrofit = retrofitHelper.getRetrofit();

        getTopBodyData apiCarbonDioxidePressureHistory = retrofit.create(getTopBodyData.class);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("patientPhone",phoneNumber);
//        Log.d("啦啦啦啦", "logData: "+jsonObject);

        Call<ResponseBody> call = apiCarbonDioxidePressureHistory.carbonDioxidePressureHistory(jsonObject);
        final Response<ResponseBody> response;
        try {
            response = call.execute();
            String jsonResult=response.body().string();
//            Log.d("啦啦啦啦", "logData: "+jsonResult);
            JSONObject result = JSON.parseObject(jsonResult);

            if (result.getInteger("code") == 1) {
                //获取的code为1 说明请求成功
                //List<BodyData> bodyData =result.getJSONArray("result").toJavaList(BodyData.class);
                List<BodyData> bodyData = JSON.parseArray((String) result.get("result"),BodyData.class);

                for(int i=0;i<bodyData.size();i++)
                {
                    Date tempTime=bodyData.get(i).getDate();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    inputTime[i]=dateFormat.format(tempTime);
                    carbonDioxidePressure[i]=bodyData.get(i).getCarbonPressure();
                }
                if(carbonDioxidePressure[0]==0)  inputTime[0]="未录入";
                showCarbonDioxidePressure();
            } else {
                //请求失败
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Toast toast=Toast.makeText(BodyHistoryCarbonDioxidePressureActivity.this, "获取动脉血二氧化碳分压数据失败，请重新尝试", Toast.LENGTH_LONG);
                        showMyToast(toast,10*500);// 设置显示时间  一次10ms,显示500次，即显示5s
                    }
                });
            }
        } catch (Exception e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BodyHistoryCarbonDioxidePressureActivity.this, "服务器在开小差儿", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }
    //Toast 自定义显示时长
    public void showMyToast(final Toast toast, final int cnt) {
        final Timer timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        },0,3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

    public void showCarbonDioxidePressure()
    {
        tvInputTime1.setText(inputTime[0]);
        tvInputTime2.setText(inputTime[1]);
        tvInputTime3.setText(inputTime[2]);
        tvInputTime4.setText(inputTime[3]);
        tvInputTime5.setText(inputTime[4]);
        tvCarbonDioxidePressureData1.setText(String.valueOf(carbonDioxidePressure[0])+"mmHg");
        tvCarbonDioxidePressureData2.setText(String.valueOf(carbonDioxidePressure[1])+"mmHg");
        tvCarbonDioxidePressureData3.setText(String.valueOf(carbonDioxidePressure[2])+"mmHg");
        tvCarbonDioxidePressureData4.setText(String.valueOf(carbonDioxidePressure[3])+"mmHg");
        tvCarbonDioxidePressureData5.setText(String.valueOf(carbonDioxidePressure[4])+"mmHg");
    }

    private void doJump()
    {
        ivReturn.setOnClickListener(view -> finish());
    }
}
